// Array.fliter(callback) 过滤出数组中符合要求的item

//const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// const res = arr.filter(item => item > 3);
// console.log(res);  // [ 4, 5, 6, 7, 8, 9 ]

Array.prototype.MyFilter = function(fn) {
    let res = [];
    if (typeof fn !== 'function') {
        return [];
    };

    // 谁调用this指向谁，this指向调用filter的arr
    for (let i = 0; i < this.length; i++) {
        if (fn(this[i])) {
            res.push(this[i]);
        };
    };

    return res;
};

console.log([1, 2, 3, 4, 5, 6, 7, 8, 9].MyFilter(item => item > 3));  // [ 4, 5, 6, 7, 8, 9 ]